import axios from 'axios'

const instance = axios.create({
  baseURL: '/api',
  timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

export const login = data => {
  return instance.post('/login', data)
}

export const getProducts = () => {
  return instance.get('/products')
}

export const getCategories = () => {
  return instance.get('/categories')
}

export const getProductDetail = id => {
  return instance.get(`/product/${id}`)
}

export const getOrders = () => {
  return instance.get('/orders')
}

// 获取轮播图
export function getBanners() {
  return axios.get('/api/banners').then(res => res.data)
} 